﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class MapManager : UIPanel
{
    private WayPoint[] allWayPoints;
    
    private void Awake()
    {
        if (UiManager == null)
        {
            GetUiManager();
        
            UiManager.m_panelStack.Push(this.gameObject);
            isActive = true;
        }

        allWayPoints = GameObject.FindObjectsOfType<WayPoint>();
    }

    private void Update()
    {
        if (isActive && Input.GetMouseButtonDown(0))
        {
            foreach (RaycastResult result in OnClickGenerate())
            {
                var wayPoint = result.gameObject.GetComponent<WayPoint>();
                if (wayPoint)
                {
                    foreach (WayPoint otherWayPoint in GameObject.FindObjectsOfType<WayPoint>())
                    {
                        if (otherWayPoint != wayPoint)
                        {
                            otherWayPoint.isSelected = false;   
                        }
                    }
                    
                    wayPoint.Click();
                    return;
                }
            }
        }
        
        CloseWithRightClick();
    }

    public override void CloseWithRightClick()
    {
        if (UiManager.m_panelStack.Count != 1)
        {
            base.CloseWithRightClick();   
        }
    }
}
